const db = wx.cloud.database();
const todos = db.collection('todos');
Page({
  data: {
    tasks: []
  },
  callFunc:function(){
    console.log('button is click')
    wx.cloud.callFunction({
      name: "connectMySQL"
    }).then(console.log)
  },
  onLoad: function (options) {
    this.getData()
  },
  onReachBottom:function(){
    this.getData()
  },
  onPullDownRefresh: function() {
    this.getData(res => {
      wx.stopPullDownRefresh();
      this.pageData.skip = 0;
    })
  },
  getData: function(callback){
    if(!callback){
      callback = res =>{}
    }
    wx.showLoading({
      title: '数据加载中',
    })
    todos.skip(this.pageData.skip).get().then(res => {
      let oldData = this.data.tasks;
      this.setData({
        tasks: oldData.concat(res.data)
      },res => {
        this.pageData.skip = this.pageData.skip + 20
        wx.hideLoading()
        callback();
      })
    })
  },
  pageData:{
    skip:0
  }
})